<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class BaseController extends Controller
{
    protected $time    = null;
    protected $openid  = null;
    protected $user_id = null;

    public function __construct()
    {
        $this->time = time();
        $this->openid = Input::get('openid');
        $this->user_id = Input::get('user_id');
    }

    public function res($params = [])
    {
        return response()->json([
            'status'    => isset($params['status']) ? $params['status'] : 200,
            'exception' => isset($params['exception']) ? $params['exception'] : 200,
            'code'      => isset($params['code']) ? $params['code'] : 0,
            'message'   => isset($params['message']) ? $params['message'] : '',
            'data'      => !empty($params['data']) ? $params['data'] : (object)array(),
        ]);
    }

    public function ok($data = [], $msg = '', $code = 0)
    {
        if (is_array($data) && empty($data)) {
            $data = (object)array();
        }
        return $this->res(['message' => $msg,'data' => $data,'code' => $code]);
    }

    public function err($code = 0, $status=200, $exception='', $msg='')
    {
        return $this->res([
            'status'    => $status,
            'exception' => $exception,
            'code'      => $code,
            'message'   => $msg
        ]);
    }

    public function verify_signature($user_id, $openid, $signature)
    {
        echo md5($user_id.$openid.env('APP_KEY'));
        if(md5($user_id.$openid.env('APP_KEY')) == $signature)
        {
            return true;
        }
        return false;
    }
}
